package com.card.client.view;

import com.card.client.dialog.DeleteDialog;
import com.card.client.dialog.EditFinanceTypeConfigDialog;
import com.card.client.entry.FinanceType;
import com.card.client.entry.Util;
import com.card.client.entry.ViewObject;
import com.card.client.service.FinanceTypeConfigService;
import com.card.client.service.FinanceTypeConfigServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.Cell;

public class FinanceTypeConfigView extends Composite implements ViewObject {

	private static FinanceTypeConfigViewUiBinder uiBinder = GWT
			.create(FinanceTypeConfigViewUiBinder.class);

	interface FinanceTypeConfigViewUiBinder extends UiBinder<Widget, FinanceTypeConfigView> {
	}

	private final FinanceTypeConfigServiceAsync fts = GWT.create(FinanceTypeConfigService.class);
	
	private Grid grid = getGrid();
	
	@UiField HorizontalPanel table;
	
	@UiField Button addFinanceType;
	
	@UiField Label descript;
	
	@UiField HTML sum;
	
	private int total;
	
	private String[] title = FinanceType.title;

	public FinanceTypeConfigView() {
		initWidget(uiBinder.createAndBindUi(this));
		table.setWidth("100%");
		table.add(grid);
		grid.setWidth("100%");
		descript.setStyleName("descript");
		TableAction action = new TableAction();
		action.setFinanceTypeConfigView(this);
		grid.addClickHandler(action);
		sum.setWidth("90%");
		sum.setStyleName("sum");
	}

	public void onLoad(){
		fts.getAllFinanceType(new AsyncCallback<FinanceType[]>() {
			public void onSuccess(FinanceType[] result) {
				if(result!=null){
					total = 0;
					setData(result);
					if(total>0) {
						sum.setText(Integer.toString(total));
					}
				}
			}
			public void onFailure(Throwable caught) {}
		});
	}
	
	private void setData(FinanceType[] result){
		if(result.length==0){
			grid.resize(1, title.length);
		} else {
			grid.resize(result.length + 1, title.length);
		}
		for(int i=0;i<title.length;i++){
			if(i == 0){
				grid.getColumnFormatter().setWidth(i, "10%");
			} else if (i == title.length-1){
				grid.getColumnFormatter().setWidth(i, "30%");
			} else {
				grid.getColumnFormatter().setWidth(i, "20%");
			}
		}
		
		for(int i=0;i<title.length;i++){
			grid.setText(0, i, title[i]);
			if(i!=(title.length-1)){
				grid.getCellFormatter().addStyleName(0, i, "header");
			} else {
				grid.getCellFormatter().addStyleName(0, i, "header-n");
			}
		}
		for(int i=0;i<result.length;i++){
			int a=0;
			FinanceType ft = result[i];
			grid.setText(i+1, a++, Long.toString(ft.getId()));
			grid.setText(i+1, a++, ft.getFinanceType());
			if(ft.getTypeIndex() == Util.FINANCE_TYPE_IS_CASH){
				grid.setText(i+1, a++, Util.FINANCE_TYPE_IS_CASH_NAME);
			} else if(ft.getTypeIndex() == Util.FINANCE_TYPE_IS_CARD){
				grid.setText(i+1, a++, Util.FINANCE_TYPE_IS_CARD_NAME);
			}
			if(ft.getTypeInOut() == Util.FINANCE_TYPE_AMOUNT_IN) {
				grid.setText(i+1, a++, Util.FINANCE_TYPE_AMOUNT_IN_NAME);
			} else if (ft.getTypeInOut() == Util.FINANCE_TYPE_AMOUNT_OUT) {
				grid.setText(i+1, a++, Util.FINANCE_TYPE_AMOUNT_OUT_NAME);
			}
			grid.setText(i+1, a++, ft.getFinanceTypeNotes());
			reload(i+1);
		}
	}
	
	@UiHandler("addFinanceType")
	void onClick(ClickEvent e) {
		EditFinanceTypeConfigDialog eftd = new EditFinanceTypeConfigDialog("添加财务类型", "添加一项财务类型", this, null, Util.DIALOG_CONTROL_ADD);
//		eftd.center();
		eftd.show();
	}

	private void reload(int row) {
		descript.setText(null);
		if(row%2!=0){
			for(int i=0;i<title.length;i++){
				if(i==title.length-1){
					grid.getCellFormatter().setStyleName(row, i, "row0-n");
				} else {
					grid.getCellFormatter().setStyleName(row, i, "row0");
				}
			}
		} else {
			for(int i=0;i<title.length;i++){
				if(i==title.length-1){
					grid.getCellFormatter().setStyleName(row, i, "row1-n");
				} else {
					grid.getCellFormatter().setStyleName(row, i, "row1");
				}
			}
		}
	}
	
	public Grid getGrid() {
		if (grid != null){
			return grid;
		}
		return new GreenMouseEventGrid();
	}
	
	class GreenMouseEventGrid extends MouseEventGrid {

		public void onMouseOut(Element td, int row) {
			reload(row);
		}

		public void onMouseOver(Element td, int row) {
			Element tr = DOM.getParent(td);
			int column = DOM.getChildIndex(tr, td);
			String name = grid.getHTML(row, 1);
			if(column == 0){
				descript.setText("删除类型 “" + name + "” ");
			} else {
				descript.setText("修改类型 “" + name + "” 信息");
			}
			for(int i=0;i<title.length;i++){
				if(i==title.length-1){
					grid.getCellFormatter().setStyleName(row, i, "rows-n");
				} else {
					grid.getCellFormatter().setStyleName(row, i, "rows");
				}
			}
		}
    }
	
	public class TableAction implements ClickHandler {
		
		private FinanceTypeConfigView financeTypeConfigView;
		
		public void onClick(ClickEvent event) {
			HTMLTable table = (HTMLTable)event.getSource();
			Cell Mycell = table.getCellForEvent(event);
			if( Mycell == null ) return;
			int row = Mycell.getRowIndex();
			int col = Mycell.getCellIndex();
			reload(row);
			FinanceType ft = getObject(row);
			Long id = Long.valueOf(grid.getHTML(row, 0));
			ft.setId(id);
			if(col == 0){
				String des = "确认删除类型 “" + ft.getFinanceType() + "” 么？";
				DeleteDialog dd = new DeleteDialog("财务类型", des, financeTypeConfigView, id);
				dd.show();
			} else {
				String des = "修改类型 “" + ft.getFinanceType() + "” 的信息";
				EditFinanceTypeConfigDialog eftd = new EditFinanceTypeConfigDialog("修改财务类型信息", des, financeTypeConfigView, ft, Util.DIALOG_CONTROL_EDIT);
				eftd.show();
			}
		}

		public void setFinanceTypeConfigView(FinanceTypeConfigView financeTypeConfigView){
			this.financeTypeConfigView = financeTypeConfigView;
		}
	}

	private FinanceType getObject(int row){
		FinanceType ft = new FinanceType();
		ft.setFinanceType(grid.getHTML(row, 1));
		if(grid.getHTML(row, 2).equals(Util.FINANCE_TYPE_IS_CASH_NAME)){
			ft.setTypeIndex(Util.FINANCE_TYPE_IS_CASH);
		} else if(grid.getHTML(row, 2).equals(Util.FINANCE_TYPE_IS_CARD_NAME)){
			ft.setTypeIndex(Util.FINANCE_TYPE_IS_CARD);
		}
		if(grid.getHTML(row, 3).equals(Util.FINANCE_TYPE_AMOUNT_IN_NAME)){
			ft.setTypeInOut(Util.FINANCE_TYPE_AMOUNT_IN);
		} else if(grid.getHTML(row, 3).equals(Util.FINANCE_TYPE_AMOUNT_OUT_NAME)){
			ft.setTypeInOut(Util.FINANCE_TYPE_AMOUNT_OUT);
		}
		ft.setFinanceTypeNotes(grid.getHTML(row, 4));
		return ft;
	}

	@Override
	public void delDate(Long id) {
		if(id == null){
			return;
		}
		fts.delFinanceType(id, new AsyncCallback<Void>() {
			public void onSuccess(Void result) {
				onLoad();
			}
			public void onFailure(Throwable caught) {}
		});
	}
}
